package study.javaee.practice.service.impl;

import cn.hutool.crypto.asymmetric.AsymmetricCrypto;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import study.javaee.practice.property.SecurityProperty;
import study.javaee.practice.service.AsymEncService;

import java.security.Key;

@Service
public class AsymEncServiceImpl implements AsymEncService {

    private final SecurityProperty securityProperty;
    private final AsymmetricCrypto asymmetricCrypto;

    @Autowired
    public AsymEncServiceImpl(SecurityProperty securityProperty) {
        this.securityProperty = securityProperty;
        asymmetricCrypto = new RSA(securityProperty.getPriKey(), securityProperty.getPubKey());
    }


    @Override
    public Key getPubKey() {
        return asymmetricCrypto.getPublicKey();
    }

    @Override
    public String getStrPubKey() {
        return securityProperty.getPubKey();
    }

    @Override
    public String decDataWithPrivateKey(String data) {
        return asymmetricCrypto.decryptStr(data, KeyType.PrivateKey);
    }

    @Override
    public String encDataWithPubKey(String data) {
        return asymmetricCrypto.encryptBase64(data, KeyType.PublicKey);
    }

}
